<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
import SelectGoods from './selectgoods.vue'
import selectwarehouse from '@/views/stocktaking/info/selectwarehouse.vue'
import confirmdial from './confirmdial.vue'
// import { getAllApi, deleteApi } from '@/api/stocktaking/info'
import { getAllApi } from '@/api/stocktaking/info'


const formData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)

// 搜索表单
const form = ref({
  targetWarehouseId: '',
  status: ''
})

// 状态选项
const statusOptions = [
  { value: 1, label: '未确认' },
  { value: 2, label: '已确认' },
  { value: 3, label: '已调整' }
]

// 类型选项
const categoryOptions = [
  { value: 1, label: '滚动盘点' },
  { value: 2, label: '定时盘点' }
]

// 弹窗引用
const diaRef = ref(null)
const selectWarehouseRef = ref(null)
const selectGoodsRef = ref(null)
// 新增：打开选择仓库
const openSelectWarehouse = () => {
  selectWarehouseRef.value.open()
}

// 处理仓库确认
const handleConfirmWarehouse = (data) => {
  // 这里可以处理选中的商品
  console.log('选中商品:', data)
  ElMessage.success('商品选择成功')
}
// 处理确认
const handleConfirm = (data) => {
  console.log('选中仓库信息:', data)
  // 可以将数据传给 dial.vue 或直接保存
}

// 处理取消
const handleCancel = () => {
  console.log('取消选择')
}
// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}


// 新增：处理商品提交
const handleConfirmGoods = (data) => {
  console.log('选中商品:', data)
  // 可以保存到表单或发起创建盘点请求
  ElMessage.success('商品选择成功')
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

// 获取数据
const getAll = async () => {
  // 模拟数据获取
  let res = await getAllApi(
    form.value.targetWarehouseId,
    form.value.status,
    currentPage.value,
    pageSize.value
  )
  total.value = res.total
  formData.value = res.rows

}

// 搜索
const search = () => {
  currentPage.value = 1
  getAll()
}

// 重置搜索
const resetSearch = () => {
  form.value = {
    targetWarehouseId: '',
    status: ''
  }
  search()
}

// 添加
const handleAdd = () => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.resetForm()
  }
}

// 编辑
const handleEdit = (row) => {
  if (confirmRef.value) {
    confirmRef.value.dialogVisible = true
    // 模拟数据回显
    confirmRef.value.dialogFormData = { ...row }
  }
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除这条记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // await deleteApi(id)
    ElMessage.success('删除成功')
    getAll()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 查看详情
const handleViewDetails = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.dialogFormData = { ...row }
    diaRef.value.isViewMode = true
  }
}
const confirmRef = ref(null)
onMounted(() => {
  getAll()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" />
  <SelectGoods ref="selectGoodsRef" @confirm="handleConfirmGoods" @cancel="handleCancel" />
  <selectwarehouse ref="selectWarehouseRef" @confirm="handleConfirm" @cancel="handleCancel" @refresh="getAll" />
  <confirmdial ref="confirmRef" />
  <!-- 搜索表单 -->
  <el-form :model="form" :inline="true" class="search-form">
    <el-form-item label="仓库名称">
      <el-input v-model="form.targetWarehouseId" placeholder="请输入仓库名称" />
    </el-form-item>
    <el-form-item label="状态" style="width: 150px;">
      <el-select v-model="form.status" placeholder="请选择状态" clearable>
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </el-form-item>
  </el-form>

  <!-- 操作按钮 -->
  <div class="toolbar">
    <el-button type="primary" @click="openSelectWarehouse">新增盘点</el-button>
  </div>

  <!-- 数据表格 -->
  <el-table :data="formData" style="width: 100%" border>
    <el-table-column prop="id" label="主键ID" width="100" />
    <el-table-column prop="targetWarehouseId" label="仓库ID" width="100" />
    <el-table-column prop="targetWarehouseAreaId" label="库区ID" width="100" />
    <el-table-column prop="targetWarehouseAreaLocaId" label="库位ID" width="100" />
    <el-table-column prop="skuId" label="规格ID" width="100" />
    <el-table-column prop="bookCount" label="账面数量" width="100" />
    <el-table-column prop="correctCount" label="盘点数量" width="100" />
    <el-table-column prop="discrepancyCount" label="差异数量" width="100">
      <template #default="{ row }">
        <el-tag :type="row.discrepancyCount < 0 ? 'danger' : row.discrepancyCount > 0 ? 'success' : 'info'">
          {{ row.discrepancyCount }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="category" label="类型" width="120">
      <template #default="{ row }">
        <el-tag :type="row.category === 1 ? 'primary' : 'warning'">
          {{ row.category === 1 ? '滚动盘点' : '定时盘点' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="状态" width="120">
      <template #default="{ row }">
        <el-tag :type="row.status === 1 ? 'info' : row.status === 2 ? 'primary' : 'success'">
          {{ row.status === 1 ? '未确认' : row.status === 2 ? '已确认' : '已调整' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column prop="createBy" label="创建人" width="120" />
    <el-table-column fixed="right" label="操作" width="200">
      <template #default="{ row }">
        <el-button type="primary" size="small" @click="handleViewDetails(row)">查看</el-button>
        <el-button type="primary" size="small" @click="handleEdit(row)">确认</el-button>
        <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="pagination-container">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 50]"
      :size="size" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<style scoped>
.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>